<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>hcc-flow 登录</title>
<link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
<link href="css/login.css" type="text/css" rel="stylesheet">
</head>
<body>
	<div class="login">
		<div class="message">hcc-flow 流程管理</div>
		<div id="darkbannerwrap"></div>

		<form id="login-form" method="post" onsubmit="return false;">
			<input id="username" name="username" placeholder="用户名" type="text" autocomplete="off" value="admin">
			<hr class="hr15">
			<input id="password" name="password" placeholder="密码" type="password" autocomplete="off" value="hccflow">
			<hr class="hr15">
			<a href="javascript:void(0);" title="点击更换验证码">
                <img id="imgVerify" name="imgVerify" src="" alt="更换验证码" height="50" width="125" onclick="getVerify(this,'N');">
            </a>
            <input id="vertifyCode" name="vertifyCode" placeholder="验证码" type="text" autocomplete="off" style="width:200px;float:right;">
			<hr class="hr15">
			<button style="width: 100%;" type="submit" onclick="login(this)">登 录</button>
			<hr class="hr20">
			<a href="javascript:void(0);" onclick="goReg()" style="display: block;">无账号,去注册</a>
			<span id="info" style="color: red;display: block;margin-top:5px;"></span>
		</form>
		<form class="form-horizontal none" onsubmit="return false" id="reg-form">
			<input type="hidden" name="isLogin" value="Y">
			<input type="hidden" name="isPhoneLogin" value="Y">
			<input type="hidden" name="status" value="C">
			<input type="hidden" name="orgId" value="1">
			<div class="form-group">
				<label class="col-md-2 control-label">账号</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入账号" type="text" name="userAcct">
				</div>
			</div>
			<hr class="hr15">
			<div class="form-group">
				<label class="col-md-2 control-label">密码</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入密码" type="text" value="" name="password">
				</div>
			</div>
			<hr class="hr15">
			<div class="form-group">
				<label class="col-md-2 control-label">确认密码</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入确认密码" type="text" value="" name="cpassword">
				</div>
			</div>
			<hr class="hr15">
			<div class="form-group">
				<label class="col-md-2 control-label">姓名</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入姓名" type="text" name="userName">
				</div>
			</div>
			<hr class="hr15 hide">
			<div class="form-group hide">
				<label class="col-md-2 control-label">公司名称</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入公司名称" type="text" name="orgName">
				</div>
			</div>
			<hr class="hr15">
			<div class="form-group">
				<label class="col-md-2 control-label">手机号</label>
				<div class="col-md-10">
					<input class="form-control" placeholder="请输入手机号" type="text" maxlength="11" name="phone">
				</div>
			</div>
			<hr class="hr15">
			<a href="javascript:void(0);" title="点击更换验证码">
                <img id="imgVerifyR" name="imgVerifyR" src="" alt="更换验证码" height="50" width="125" onclick="getVerify(this,'Y');">
            </a>
            <input id="verifyCodeR" name="verifyCodeR" placeholder="请输入验证码" type="text" autocomplete="off" style="width:200px;float:right;">
			<hr class="hr15">
			<button style="width: 100%;" type="submit" onclick="reg()">注 册</button>
			<hr class="hr20">
			<a href="javascript:void(0);" onclick="goLogin()">返回直接登录</a>
			
		</form>
	</div>
</body>
 <script type="text/javascript" src="js/libs/jquery-2.1.1.min.js"></script>
 <script type="text/javascript" src="js/common.js"></script>
 <script type="text/javascript" src="js/jquery.cookie.js"></script>
 <script type="text/javascript" src="layui/layui.js"></script>
 <script type="text/javascript">
 	layui.use(['layer'], function(){
	    var layer = layui.layer;
	});
	if (top != self) {
		parent.location.href = urlRootPrefix + '/login.html';
	}
	//获取验证码
	function getVerify(obj,isReg){
	    obj.src = urlRootPrefix + "/verify/getcode?isReg="+isReg+"&t="+Math.random();
	}
	
	getVerify(document.getElementById("imgVerify"));
	
	var token = localStorage.getItem(urlRootPrefix.replace("/","") + "token");
	if (token != null && token.trim().length != 0) {
		$.ajax({
			type : 'get',
			url : urlRootPrefix + '/users/current?token=' + token,
			success : function(data) {
				location.href = urlRootPrefix + '/index.html';
			},
			error : function(xhr, textStatus, errorThrown) {
				var msg = xhr.responseText;
				var response = JSON.parse(msg);
				var code = response.code;
				var message = response.message;
				if (code == 401) {
					localStorage.removeItem(urlRootPrefix.replace("/","") + "token");
				}
			}
		});
	}
	function getCookie(name){
		var strcookie = document.cookie;//获取cookie字符串
		//alert(strcookie);
		var arrcookie = strcookie.split("; ");//分割
		//遍历匹配
		for ( var i = 0; i < arrcookie.length; i++) {
		var arr = arrcookie[i].split("=");
		if (arr[0] == name){
		return arr[1];
		}
		}
		return "";
	}
	function login(obj) {
		$(obj).attr("disabled", true);
		var code = $.trim($('#vertifyCode').val());
		var codeId = getCookie('VerifyId');
		var username = $.trim($('#username').val());
		var password = $.trim($('#password').val());
		if (username == "" || password == "") {
			$("#info").html('用户名或者密码不能为空');
			$(obj).attr("disabled", false);
		} else {
			$.ajax({
				type : 'post',
				url : urlRootPrefix + '/login',
				contentType:'application/json',
				data : JSON.stringify({"username":username,"verifyId":codeId,"verifyCode":code,"password":password}),
				success : function(data) {
					//console.log(data);
					if(data.statusCode == "0"){
						//debugger;
						var dd = data.data;
						var cc = jQuery.parseJSON(dd);
						//alert(cc.token);
						localStorage.setItem(urlRootPrefix.replace("/","") + "token", cc.token);
						location.href = urlRootPrefix + '/index.html';
					}else{
						$("#info").html(data.msg);
						getVerify(document.getElementById("imgVerify"));
						$("#vertifyCode").val("");
						$(obj).attr("disabled", false);
					}
				},
				error : function(xhr, textStatus, errorThrown) {
					var msg = xhr.responseText;
					alert(msg);
					var response = JSON.parse(msg);
					$("#info").html(response.message);
					$(obj).attr("disabled", false);
				}
			});
		}
	}
	function goReg(){
		$("#login-form").addClass("none");
		$("#reg-form").removeClass("none");
		$(".login").addClass("reg");
		getVerify(document.getElementById("imgVerifyR"),"Y");
	}
	function goLogin(){
		$("#reg-form").addClass("none");
		$("#login-form").removeClass("none");
		$(".login").removeClass("reg");
		getVerify(document.getElementById("imgVerify"),"N");
	}
	function reg(obj) {
		$(obj).attr("disabled", true);
	    var formdata = $("#reg-form").serializeObject();
	    if(!formdata.userAcct){
	    	layer.msg("用户名不能为空");
	    	$(obj).attr("disabled", false);
	    	return;
	    }
	    if(!formdata.password){
	    	layer.msg("密码不能为空");
	    	$(obj).attr("disabled", false);
	    	return;
	    }
	    if(!formdata.cpassword){
	    	layer.msg("确认密码不能为空");
	    	$(obj).attr("disabled", false);
	    	return;
	    }
	    if(formdata.password != formdata.cpassword){
	    	layer.msg("两次密码不一致");
	    	$(obj).attr("disabled", false);
	    	return;
	    }
	    formdata.verifyIdR =  getCookie('VerifyIdR');
		$.ajax({
			type : 'post',
			url : urlRootPrefix + '/users/loginReg',
			contentType: "application/json; charset=utf-8",  
			data : JSON.stringify(formdata),
			success : function(data) {
				if(!data.success){
					layer.msg(data.msg, {shift: -1, time: 3000}, function(){});
					$("button[type='submit']").removeAttr("disabled");
					$(obj).attr("disabled", false);
					return;
				}
				layer.msg("注冊成功,请登录", {shift: -1, time: 1000}, function(){
					goLogin();
					$("#username").val(formdata.userAcct);
                });
			}
		});
	}
</script>
</html>